home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / item-file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-23  |  5.0 KB  |  225 lines

  1. /* cfengine for GNU
  2.  
  3.         Copyright (C) 1995
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained 
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program; if not, write to the Free Software
  22.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  
  24. */
  25.  
  26.  
  27. /*********************************************************************/
  28. /*                                                                   */
  29. /*  TOOLKIT: the "item file extension" object for cfengine           */
  30. /*                                                                   */
  31. /*********************************************************************/
  32.  
  33. #include "cf.defs.h"
  34. #include "cf.extern.h"
  35.  
  36. /*********************************************************************/
  37.  
  38. LoadItemList(liststart,file)
  39.  
  40. struct Item **liststart;
  41. char *file;
  42.  
  43. { FILE *fp;
  44.   struct stat statbuf;
  45.  
  46. if (stat(file,&statbuf) == -1)
  47.    {
  48.    sprintf(OUTPUT,"Couldn't stat %s\n",file);
  49.    CfLog(cfverbose,OUTPUT,"stat");
  50.    return false;
  51.    }
  52.  
  53. if ((EDITFILESIZE != 0) &&(statbuf.st_size > EDITFILESIZE))
  54.    {
  55.    sprintf(OUTPUT,"File %s is bigger than the limit <editfilesize>\n",file);
  56.    CfLog(cfinform,OUTPUT,"");
  57.    return(false);
  58.    }
  59.  
  60. if (! S_ISREG(statbuf.st_mode))
  61.    {
  62.    sprintf(OUTPUT,"%s is not a plain file\n",file);
  63.    CfLog(cfinform,OUTPUT,"");
  64.    return false;
  65.    }
  66.  
  67. if ((fp = fopen(file,"r")) == NULL)
  68.    {
  69.    sprintf(OUTPUT,"Couldn't read file %s for editing\n",file);
  70.    CfLog(cferror,OUTPUT,"fopen");
  71.    return false;
  72.    }
  73.  
  74.  
  75. bzero(VBUFF,bufsize); 
  76.  
  77. while(!feof(fp))
  78.    {
  79.    ReadLine(VBUFF,bufsize,fp);
  80.  
  81.    if (!feof(fp) || (strlen(VBUFF) != 0))
  82.       {
  83.       AppendItem(liststart,VBUFF,NULL);
  84.       }
  85.    VBUFF[0] = '\0';
  86.    }
  87.  
  88. fclose(fp);
  89. return (true); 
  90. }
  91.  
  92. /*********************************************************************/
  93.  
  94. SaveItemList(liststart,file)
  95.  
  96. struct Item *liststart;
  97. char *file;
  98.  
  99. { struct Item *ip;
  100.   struct stat statbuf;
  101.   FILE *fp;
  102.  
  103. if (stat(file,&statbuf) == -1)
  104.    {
  105.    sprintf(OUTPUT,"Couldn't stat %s, which needed editing!\n",file);
  106.    CfLog(cferror,OUTPUT,"");
  107.    CfLog(cferror,"Check definition in program - is file NFS mounted?\n\n","");
  108.    return false;
  109.    }
  110.  
  111. strcpy(VBUFF,file);
  112. strcat(VBUFF,CF_EDITED);
  113.  
  114. if (! IsItemIn(VREPOSLIST,VBUFF))
  115.    {
  116.    if (rename(file,VBUFF) == -1)
  117.       {
  118.       sprintf(OUTPUT,"Error while renaming %s\n",file);
  119.       CfLog(cferror,OUTPUT,"rename ");
  120.       return false;
  121.       }
  122.    else if (Repository(VBUFF))
  123.       {
  124.       unlink(VBUFF);
  125.       }
  126.    }
  127.  
  128. unlink(file); /* Just in case of races */ 
  129.  
  130. if ((fp = fopen(file,"w")) == NULL)
  131.    {
  132.    sprintf(OUTPUT,"Couldn't write file %s after editing\n",file);
  133.    rename(VBUFF,file);
  134.    CfLog(cferror,OUTPUT,"");
  135.    return false;
  136.    }
  137.  
  138. for (ip = liststart; ip != NULL; ip=ip->next)
  139.    {
  140.    fprintf(fp,"%s\n",ip->name);
  141.    }
  142.  
  143. sprintf(OUTPUT,"Edited file %s \n",file);
  144. CfLog(cfinform,OUTPUT,""); 
  145.  
  146. fclose(fp);
  147. chmod(file,statbuf.st_mode);                    /* Restore file permissions etc */
  148. chown(file,statbuf.st_uid,statbuf.st_gid);
  149. return true;
  150. }
  151.  
  152. /*********************************************************************/
  153.  
  154. CompareToFile(liststart,file)
  155.  
  156. /* returns true if file on disk is identical to file in memory */
  157.  
  158. struct Item *liststart;
  159. char *file;
  160.  
  161. { FILE *fp;
  162.   struct stat statbuf;
  163.   struct Item *ip = liststart;
  164.  
  165. Debug("CompareToFile(%s)\n",file);
  166.  
  167. if (stat(file,&statbuf) == -1)
  168.    {
  169.    return false;
  170.    }
  171.  
  172. if (liststart == NULL)
  173.    {
  174.    return false;
  175.    }
  176.  
  177. if ((fp = fopen(file,"r")) == NULL)
  178.    {
  179.    sprintf(OUTPUT,"Couldn't read file %s for editing\n",file);
  180.    CfLog(cferror,OUTPUT,"fopen");
  181.    return false;
  182.    }
  183.  
  184. bzero(VBUFF,bufsize);
  185.  
  186. for (ip = liststart; ip != NULL; ip=ip->next)
  187.    {
  188.    ReadLine(VBUFF,bufsize,fp);
  189.    
  190.    if (feof(fp) && (ip->next != NULL))
  191.       {
  192.       fclose(fp);
  193.       return false;
  194.       }
  195.  
  196.    if ((ip->name == NULL) && (strlen(VBUFF) == 0))
  197.       {
  198.       continue;
  199.       }
  200.    
  201.    if (ip->name == NULL)
  202.       {
  203.       fclose(fp);
  204.       return false;
  205.       }
  206.    
  207.    if (strcmp(ip->name,VBUFF) != 0)
  208.       {
  209.       fclose(fp);
  210.       return false;
  211.       }
  212.  
  213.    VBUFF[0] = '\0';
  214.    }
  215.  
  216. if (!feof(fp))
  217.    {
  218.    fclose(fp);
  219.    return false;
  220.    }
  221.  
  222. fclose(fp);
  223. return (true);
  224. }
  225.